<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="cTitle">
        <el-input
          v-model="queryParams.cTitle"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="副标题" prop="cSubtitle">
        <el-input
          v-model="queryParams.cSubtitle"
          placeholder="请输入副标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示状态" prop="showState">
        <el-select v-model="queryParams.showState" placeholder="请选择显示状态" clearable>
          <el-option
            v-for="dict in dict.type.content_show_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="父分类" prop="fatherType">
        <el-select v-model="queryParams.fatherType" placeholder="请选择父分类" clearable>
          <el-option
            v-for="dict in dict.type.content_father_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="子分类" prop="childType">
        <el-select v-model="queryParams.childType" placeholder="请选择子分类" clearable>
          <el-option
            v-for="dict in dict.type.content_child_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="语言" prop="dataLanguage">
        <el-select v-model="queryParams.dataLanguage" placeholder="请选择语言" clearable>
          <el-option
            v-for="dict in dict.type.content_language_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:content:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:content:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:content:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:content:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="cTitle" />
      <el-table-column label="副标题" align="center" prop="cSubtitle" />
      <el-table-column label="简介" align="center" prop="cIntroduction" />
      <el-table-column label="封面图" align="center" prop="cCover" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cCover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="显示状态" align="center" prop="showState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.content_show_state" :value="scope.row.showState"/>
        </template>
      </el-table-column>
      <el-table-column label="父分类" align="center" prop="fatherType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.content_father_type" :value="scope.row.fatherType"/>
        </template>
      </el-table-column>
      <el-table-column label="子分类" align="center" prop="childType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.content_child_type" :value="scope.row.childType"/>
        </template>
      </el-table-column>
      <el-table-column label="语言" align="center" prop="dataLanguage">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.content_language_type" :value="scope.row.dataLanguage"/>
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" align="center" prop="showSort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:content:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:content:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通用内容管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="标题" prop="cTitle">
          <el-input v-model="form.cTitle" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="副标题" prop="cSubtitle">
          <el-input v-model="form.cSubtitle" placeholder="请输入副标题" />
        </el-form-item>
        <el-form-item label="富文本正文">
          <editor v-model="form.cContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="简介" prop="cIntroduction">
          <el-input v-model="form.cIntroduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="封面图" prop="cCover">
          <!-- <image-upload :limit="1" v-model="form.cCover"/> -->
          <cropper :value="form.cCover" :limit="1" @getImg="getImg"></cropper>
        </el-form-item>
        <el-form-item label="图片展示" prop="cPictures">
          <image-upload :limit="20" v-model="form.cPictures"/>
        </el-form-item>
        <el-form-item label="相关附件" prop="cFiles">
          <file-upload v-model="form.cFiles"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="父分类" prop="fatherType">
              <el-select v-model="form.fatherType" placeholder="请选择父分类" style="width:100%">
                <el-option
                  v-for="dict in dict.type.content_father_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="子分类" prop="childType">
              <el-select v-model="form.childType" placeholder="请选择子分类" style="width:100%">
                <el-option
                  v-for="dict in dict.type.content_child_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="显示状态" prop="showState">
              <el-select v-model="form.showState" placeholder="请选择显示状态" style="width:100%">
                <el-option
                  v-for="dict in dict.type.content_show_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示顺序" prop="showSort">
              <el-input v-model="form.showSort" placeholder="请输入显示顺序" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label="语言" prop="dataLanguage">
              <el-select v-model="form.dataLanguage" placeholder="请选择语言" style="width:100%">
                <el-option
                  v-for="dict in dict.type.content_language_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布日期" prop="cDate" >
              <el-date-picker clearable style="width:100%"
                v-model="form.cDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择发布日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="URL链接" prop="cUrl">
          <el-input v-model="form.cUrl" placeholder="请输入URL链接" />
        </el-form-item>
        <el-form-item label="内容来源" prop="cSource">
          <el-input v-model="form.cSource" placeholder="请输入内容来源" />
        </el-form-item>
        <el-form-item label="内容作者" prop="cAuthor">
          <el-input v-model="form.cAuthor" placeholder="请输入内容作者" />
        </el-form-item>
        <el-form-item label="数量维护" prop="cNum">
          <el-input v-model="form.cNum" placeholder="请输入数量维护" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContent, getContent, delContent, addContent, updateContent } from "@/api/system/content";

export default {
  name: "Content",
  dicts: ['content_father_type', 'content_child_type', 'content_show_state', 'content_language_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通用内容管理表格数据
      contentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cTitle: null,
        cSubtitle: null,
        cContent: null,
        cIntroduction: null,
        cCover: null,
        cPictures: null,
        cFiles: null,
        showState: null,
        showSort: null,
        fatherType: null,
        childType: null,
        dataLanguage: null,
        cUrl: null,
        cDate: null,
        cSource: null,
        cAuthor: null,
        cNum: null,
        deptId: null,
        companyId: null,
        extend1: null,
        extend2: null,
        extend3: null,
        extend4: null,
        extend5: null,
        extend6: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getImg(data) {
        this.form.cCover = data.toString()
        console.log(this.form.cCover, 'cCover')
    },
    /** 查询通用内容管理列表 */
    getList() {
      this.loading = true;
      listContent(this.queryParams).then(response => {
        this.contentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cTitle: null,
        cSubtitle: null,
        cContent: null,
        cIntroduction: null,
        cCover: null,
        cPictures: null,
        cFiles: null,
        showState: '1',
        showSort: 1,
        fatherType: null,
        childType: null,
        dataLanguage: 'zh',
        cUrl: null,
        cDate: null,
        cSource: null,
        cAuthor: null,
        cNum: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptId: null,
        companyId: null,
        extend1: null,
        extend2: null,
        extend3: null,
        extend4: null,
        extend5: null,
        extend6: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加通用内容管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改通用内容管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除通用内容管理编号为"' + ids + '"的数据项？').then(function() {
        return delContent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/content/export', {
        ...this.queryParams
      }, `content_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
